/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadastrarUsuario.java
 *
 * Created on 11/03/2011, 14:12:01
 */

package pbf.dialog;

import javax.swing.JOptionPane;
import org.hibernate.Session;
import pbf.conf.hibernate.HibernateUtil;
import pbf.conf.hibernate.entidades.Usuario;
import pbf.util.DataHora;
import pbf.util.ValidaCpf;






/**
 *
 * @author User
 */
public class CadastrarUsuario extends javax.swing.JDialog {

    /** Creates new form CadastrarUsuario */
    public CadastrarUsuario(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        usuarioNome = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel19 = new javax.swing.JLabel();
        usuarioLogin = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        usuarioConSenha = new javax.swing.JPasswordField();
        usuarioSenha = new javax.swing.JPasswordField();
        usuarioNivelAcessor = new javax.swing.JComboBox();
        jSeparator2 = new javax.swing.JSeparator();
        jButton1 = new javax.swing.JButton();
        usuarioCpf = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(null);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 16));
        jLabel4.setText("Dados do Usúario");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(20, 20, 170, 20);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel3.setText("Nome:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(20, 50, 170, 20);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel2.setText("Nível de Acessor:");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(20, 200, 180, 20);

        usuarioNome.setFont(new java.awt.Font("Tahoma", 0, 14));
        getContentPane().add(usuarioNome);
        usuarioNome.setBounds(190, 50, 370, 23);
        getContentPane().add(jSeparator1);
        jSeparator1.setBounds(20, 40, 540, 10);

        jLabel19.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel19.setText("CPF:");
        getContentPane().add(jLabel19);
        jLabel19.setBounds(20, 80, 170, 20);

        usuarioLogin.setFont(new java.awt.Font("Tahoma", 0, 14));
        getContentPane().add(usuarioLogin);
        usuarioLogin.setBounds(190, 110, 370, 23);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel5.setText("Login:");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(20, 110, 170, 20);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel6.setText("Senha:");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(20, 140, 170, 20);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel7.setText("Confirmar Senha:");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(20, 170, 170, 20);

        usuarioConSenha.setFont(new java.awt.Font("Tahoma", 0, 14));
        getContentPane().add(usuarioConSenha);
        usuarioConSenha.setBounds(190, 170, 370, 20);

        usuarioSenha.setFont(new java.awt.Font("Tahoma", 0, 14));
        getContentPane().add(usuarioSenha);
        usuarioSenha.setBounds(191, 140, 370, 20);

        usuarioNivelAcessor.setFont(new java.awt.Font("Tahoma", 0, 14));
        usuarioNivelAcessor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Secretária do Programa Bolsa Família/Cadastramento Único", "Atendente do Programa Bolsa Família/Cadastramento Único", "Administrador do Sistema" }));
        getContentPane().add(usuarioNivelAcessor);
        usuarioNivelAcessor.setBounds(190, 200, 370, 23);
        getContentPane().add(jSeparator2);
        jSeparator2.setBounds(0, 440, 620, 10);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/user_add.png"))); // NOI18N
        jButton1.setText("Cadastrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(423, 450, 140, 25);

        try {
            usuarioCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        usuarioCpf.setFont(new java.awt.Font("Tahoma", 0, 14));
        getContentPane().add(usuarioCpf);
        usuarioCpf.setBounds(190, 80, 370, 23);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-629)/2, (screenSize.height-523)/2, 629, 523);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        if(usuarioNome.getText().equals(""))
        {
           JOptionPane.showMessageDialog(null,"Preencha o campo Nome!");
        }
        else
        {
            if(!ValidaCpf.validaCpf(usuarioCpf.getText()))
            {
                JOptionPane.showMessageDialog(null,"CPF inválido!");
            }
            else
            {
                 if(usuarioLogin.getText().equals(""))
                 {
                    JOptionPane.showMessageDialog(null,"Preencha o campo Login!");
                }
                else
                {
                       if(usuarioSenha.getText().equals(""))
                       {
                           JOptionPane.showMessageDialog(null,"Preencha o campo Senha!");
                       }
                       else
                       {
                           if(usuarioConSenha.getText().equals(""))
                           {
                               JOptionPane.showMessageDialog(null,"Preencha o campo Confirma Senha!");
                           }
                           else
                           {
                               if(ValidaCpf.validaCpf(usuarioCpf.getText()))
                               {
                                    if(usuarioSenha.getText().equals(usuarioConSenha.getText()))
                                    {
                                        Usuario usuario = new Usuario();
                                        usuario.setUsuarioCpf(usuarioCpf.getText());
                                        usuario.setUsuarioNome(usuarioNome.getText());
                                        usuario.setUsuarioLogin(usuarioLogin.getText());
                                        usuario.setUsuarioSenha(usuarioSenha.getText());

                                        if(usuarioNivelAcessor.getSelectedIndex()==0)
                                        {
                                            usuario.setUsuarioNivelAcesso("Administrador");
                                        }
                                   
                                        if(usuarioNivelAcessor.getSelectedIndex()==1)
                                        {
                                            usuario.setUsuarioNivelAcesso("Atendente");
                                        }
                                   
                                        if(usuarioNivelAcessor.getSelectedIndex()==2)
                                        {
                                            usuario.setUsuarioNivelAcesso("Secretária");
                                        }

                                        usuario.setUsuarioAtivo("S");
                                        usuario.setUsuarioBloqueado("N");
                                        usuario.setUsuarioDtAlteracao(DataHora.getCalendar().getTime());
                                        usuario.setUsuarioDtCadastro(DataHora.getCalendar().getTime());

                                        Session sessao = HibernateUtil.openSession();
                                        sessao.beginTransaction();
                                        sessao.save(usuario);
                                        sessao.getTransaction().commit();
                                        JOptionPane.showMessageDialog(null, "Usuario cadastrado com sucesso!");
                                        dispose();
                                   }
                                    else
                                        JOptionPane.showMessageDialog(null,"Senhas diferetes!");
                           }
                            else
                                JOptionPane.showMessageDialog(null,"CPF Invalido!");
                         }
                    }
                }
           }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                CadastrarUsuario dialog = new CadastrarUsuario(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JPasswordField usuarioConSenha;
    private javax.swing.JFormattedTextField usuarioCpf;
    private javax.swing.JTextField usuarioLogin;
    private javax.swing.JComboBox usuarioNivelAcessor;
    private javax.swing.JTextField usuarioNome;
    private javax.swing.JPasswordField usuarioSenha;
    // End of variables declaration//GEN-END:variables

}
